// pages/bindMobile/bindMobile.js
const wxaUserService = require('../../api/wxaUserService.js')
const smsService = require('../../api/smsService.js')
const auth = require('../../utils/auth.js')

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    captcha: '',
    getCodeBtn: "获取验证码",
    getCodeBgColor: '#FFA300'
  },
  bindMobileInput: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  bindcaptchaInput: function (e) {
    this.setData({
      captcha: e.detail.value
    })
  },
  getCaptcha: function () {
    if (this.checkMobile() && this.data.getCodeBgColor === '#FFA300') {
      var seconds = 60;
      var self = this;
      this.setData({
        getCodeBgColor: '#bbb',
        getCodeBtn: seconds + 's'
      })
      var time = setInterval(function () {
        seconds--;
        self.setData({
          getCodeBtn: seconds + 's'
        })
        if (seconds <= 0) {
          self.setData({
            getCodeBtn: '获取验证码',
            getCodeBgColor: '#FFA300'
          })
          clearInterval(time)
        }
      }, 1000)
      smsService.sendSms(this.data.mobile).then(res => {
        console.log(res)
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  // 检查手机号
  checkMobile: function () {
    let reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
    if (!this.data.mobile) {
      wx.showToast({
        title: "请填写手机号",
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (!reg.test(this.data.mobile)) {
      wx.showToast({
        title: "请填写正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      return true;
    }
  },
  checkCaptcha: function () {
    if (!this.data.captcha) {
      wx.showToast({
        title: "请填写验证码",
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      return true;
    }
  },
  // 前往广告页
  toAdPage: function (e) {
    // 链接
    const url = 'https://xianfengxcx.gomoretech.com/newretail-admin/#/xfAgreement'
    try {
      // let webUrl = url + '&storeId=' + this.data.storeId + '&type=normal'
      let webUrl = url
      wx.setStorageSync('webUrl', webUrl)
    } catch (e) { }
    wx.navigateTo({
      url: '../advertising/advertising?from=bindMobile'
    })
  },

  /**
   * 绑定用户手机号
   */
  bindMobile: function (val) {
    console.log(val)
    const that = this
    const loginStatus = this.checkAuth()
    if (loginStatus) {
      if (this.checkMobile() && this.checkCaptcha() && this.data.getCodeBgColor !== '#009F55') {
        try {
          wx.setStorageSync('wj_wxa_formId', val.detail.formId)
        } catch (e) {
        }
        wxaUserService.bindMobile(this.data.mobile, this.data.captcha).then(res => {
          console.log(res)
          that.getLoginInfo()
        }).catch(err => {
            wx.showToast({
              title: err.message,
              icon: 'none',
              duration: 2000
            })
        })
      } else {
        if (this.data.getCodeBgColor === '#009F55') {
          wx.showToast({
            title: '请获取验证码！',
            icon: 'none',
            duration: 2000
          })
        }
      }
    } else {
      wx.showToast({
        title: '您还未授权登陆，请登陆后绑定手机号哦~',
        icon: 'none',
        duration: 2000
      })
    }
  },
  getLoginInfo: function () {
    const that = this
    wxaUserService.login().then(res => {
      console.log(res)
      if (res.member) {
        auth.setUser(res)
        wx.showToast({
          title: '绑定成功',
          icon: 'none',
          duration: 2000
        })
        app.globalData.userInfo = res
      } else {
        wx.showToast({
          title: '绑定失败',
          icon: 'none',
          duration: 2000
        })
      }
      setTimeout(() => {
        wx.navigateTo({
          url: '../index/index',
        })
      }, 2000);
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    });
  },
  // 检查当前用户是否登录授权
  checkAuth: function () {
    if (!app.globalData.userInfo) {
      return false;
    } else {
      return true;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    this.getMemberUserInfo()
  },
  getMemberUserInfo: function () {
    if (!app.globalData.userInfo) {
      wx.showToast({
        title: "您还未授权，请登陆授权",
        icon: 'none',
        duration: 2000
      })
    } else {

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})